package com.app.business.service;

import com.app.business.calculate.CalculateUtils;
import com.app.business.model.*;
import com.app.system.jpa.JpaBaseDao;
import com.app.system.jpa.PageBean;
import com.app.system.jpa.Wrapper;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.WebUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.google.common.collect.Maps;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 征收项目管理
 * Created by ddz on 2019/11/14 10:45.
 */
@Service
@Transactional
public class ParameterService extends JpaBaseDao<Parameter, Integer> {

    @Autowired
    private OverAreaPriceNewService overAreaPriceNewService;
    @Autowired
    private OverAreaOneFloorSyPriceService overAreaOneFloorSyPriceService;
    @Autowired
    private OverAreaTwoFloorSyPriceService overAreaTwoFloorSyPriceService;
    @Autowired
    private OverAreaXzlPriceService overAreaXzlPriceService;
    @Autowired
    private OverAreaScPriceService overAreaScPriceService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private SyKgInfoService syKgInfoService;


    /**
     * 得到参数配置
     *
     * @return
     */
    public List<Parameter> parameterList() {
        //查询语句
        StringBuffer hql = new StringBuffer("from Parameter p order by p.id desc,p.name ");
        Map<String,Object> map = new HashMap<>();
        return this.findHql(hql.toString(),map);
    }

    /**
     * 分页处理
     *
     * @return
     */
    public PageBean<Parameter> getParameterPageBean() {
        //查询语句
        StringBuffer query = new StringBuffer(" p from Parameter p ");

        //默认排序(PageBean指定排序字段类型的话，则默认排序不使用)
        StringBuffer orderBy = new StringBuffer("p.id desc,p.name");
        Wrapper wrapper = new Wrapper(query, orderBy);
        wrapper.initSearchParams();
        return this.pageHql(wrapper);
    }

    /**
     * 征收项目参数配置数据保存
     * @param request
     * @return
     */
    public synchronized Response dataFormSave(HttpServletRequest request){
        int parameterId = ParamUtils.getInt(request, "parameterId", 0);
        Parameter parameter = this.findOne(parameterId);
        if (parameter == null) {
            parameter = new Parameter();
        }
        WebUtils.bind(request, parameter);

        Precondition.checkAjaxArguement(parameter != null,"1111","未找到征收项目，保存失败！");

        this.save(parameter);
        //保存之前先删除数据【住宅扩购】
        this.overAreaPriceNewService.deleteOverAreaNewPriceByParameterId(parameter.getId());

        String[] minAreaArray = ParamUtils.getStringParameters(request,"minArea");    //最小面积
        String[] maxAreaArray = ParamUtils.getStringParameters(request,"maxArea");    //最大面积
        String[] priceArray = ParamUtils.getStringParameters(request,"price");    //购房单价
        String[] sortNumArray = ParamUtils.getStringParameters(request,"sortNum");    //排序号
        //必须不为空，且长度一致（必填）
        Precondition.checkAjaxArguement(minAreaArray != null && maxAreaArray != null && minAreaArray.length == maxAreaArray.length,"2222","面积区间数据填写不规范！");
        int length = minAreaArray.length;
        String minArea = null,maxArea = null,price = null,sortNum = null;
        for (int i = 0; i < length; i++){
            minArea = null;maxArea = null;price = null;sortNum = null;
            minArea = minAreaArray[i];maxArea = maxAreaArray[i];price = priceArray[i];sortNum = sortNumArray[i];
            if (minArea != null && !minArea.equals("") && maxArea != null && !maxArea.equals("") && price != null && !price.equals("") && sortNum != null && !sortNum.equals("")){
                OverAreaPriceNew overAreaPrice = new OverAreaPriceNew();
                overAreaPrice.setMinArea(BigDecimalUtils.stringToBigDecimal(minArea));   //最小面积
                overAreaPrice.setMaxArea(BigDecimalUtils.stringToBigDecimal(maxArea));   //最大面积
                overAreaPrice.setPrice(BigDecimalUtils.stringToBigDecimal(price));   //补偿单价
                overAreaPrice.setSortNum(Integer.valueOf(sortNum));    //排序号
                overAreaPrice.setParameter(parameter);   //征收项目
                this.overAreaPriceNewService.save(overAreaPrice);
            }
        }

        //保存之前先删除数据【一层商业扩购】
        this.overAreaOneFloorSyPriceService.deleteOneFloorSyByParameterId(parameter.getId());

        String[] oneFloorSyMinAreaArray = ParamUtils.getStringParameters(request,"oneFloorSyMinArea");    //最小面积
        String[] oneFloorSyMaxAreaArray = ParamUtils.getStringParameters(request,"oneFloorSyMaxArea");    //最大面积
        String[] oneFloorSyPriceArray = ParamUtils.getStringParameters(request,"oneFloorSyPrice");    //购房单价
        String[] oneFloorSySortNumArray = ParamUtils.getStringParameters(request,"oneFloorSySortNum");    //排序号
        //必须不为空，且长度一致（必填）
        Precondition.checkAjaxArguement(oneFloorSyMinAreaArray != null && oneFloorSyMaxAreaArray != null && oneFloorSyMinAreaArray.length == oneFloorSyMaxAreaArray.length,"2222","面积区间数据填写不规范！");
        int oneFloorSylength = oneFloorSyMinAreaArray.length;
        String oneFloorSyMinArea = null,oneFloorSyMaxArea = null,oneFloorSyPrice = null,oneFloorSySortNum = null;
        for (int i = 0; i < oneFloorSylength; i++){
            oneFloorSyMinArea = null;oneFloorSyMaxArea = null;oneFloorSyPrice = null;oneFloorSySortNum = null;
            oneFloorSyMinArea = oneFloorSyMinAreaArray[i];oneFloorSyMaxArea = oneFloorSyMaxAreaArray[i];oneFloorSyPrice = oneFloorSyPriceArray[i];oneFloorSySortNum = oneFloorSySortNumArray[i];
            if (oneFloorSyMinArea != null && !oneFloorSyMinArea.equals("") && oneFloorSyMaxArea != null && !oneFloorSyMaxArea.equals("") && oneFloorSyPrice != null && !oneFloorSyPrice.equals("") && oneFloorSySortNum != null && !oneFloorSySortNum.equals("")){
                OverAreaOneFloorSyPrice overAreaOneFloorSyPrice = new OverAreaOneFloorSyPrice();
                overAreaOneFloorSyPrice.setMinArea(BigDecimalUtils.stringToBigDecimal(oneFloorSyMinArea));   //最小面积
                overAreaOneFloorSyPrice.setMaxArea(BigDecimalUtils.stringToBigDecimal(oneFloorSyMaxArea));   //最大面积
                overAreaOneFloorSyPrice.setPrice(BigDecimalUtils.stringToBigDecimal(oneFloorSyPrice));   //补偿单价
                overAreaOneFloorSyPrice.setSortNum(Integer.valueOf(oneFloorSySortNum));    //排序号
                overAreaOneFloorSyPrice.setParameter(parameter);   //征收项目
                this.overAreaOneFloorSyPriceService.save(overAreaOneFloorSyPrice);
            }
        }

        //保存之前先删除数据【二层商业扩购】
        this.overAreaTwoFloorSyPriceService.deleteOverAreaTwoFloorSyPriceByParameterId(parameter.getId());

        String[] twoFloorSyMinAreaArray = ParamUtils.getStringParameters(request,"twoFloorSyMinArea");    //最小面积
        String[] twoFloorSyMaxAreaArray = ParamUtils.getStringParameters(request,"twoFloorSyMaxArea");    //最大面积
        String[] twoFloorSyPriceArray = ParamUtils.getStringParameters(request,"twoFloorSyPrice");    //购房单价
        String[] twoFloorSySortNumArray = ParamUtils.getStringParameters(request,"twoFloorSySortNum");    //排序号
        //必须不为空，且长度一致（必填）
        Precondition.checkAjaxArguement(twoFloorSyMinAreaArray != null && twoFloorSyMaxAreaArray != null && twoFloorSyMinAreaArray.length == twoFloorSyMaxAreaArray.length,"2222","面积区间数据填写不规范！");
        int twoFloorSylength = twoFloorSyMinAreaArray.length;
        String twoFloorSyMinArea = null,twoFloorSyMaxArea = null,twoFloorSyPrice = null,twoFloorSySortNum = null;
        for (int i = 0; i < twoFloorSylength; i++){
            twoFloorSyMinArea = null;twoFloorSyMaxArea = null;twoFloorSyPrice = null;twoFloorSySortNum = null;
            twoFloorSyMinArea = twoFloorSyMinAreaArray[i];twoFloorSyMaxArea = twoFloorSyMaxAreaArray[i];twoFloorSyPrice = twoFloorSyPriceArray[i];twoFloorSySortNum = twoFloorSySortNumArray[i];
            if (twoFloorSyMinArea != null && !twoFloorSyMinArea.equals("") && twoFloorSyMaxArea != null && !twoFloorSyMaxArea.equals("") && twoFloorSyPrice != null && !twoFloorSyPrice.equals("") && twoFloorSySortNum != null && !twoFloorSySortNum.equals("")){
                OverAreaTwoFloorSyPrice overAreaTwoFloorSyPrice = new OverAreaTwoFloorSyPrice();
                overAreaTwoFloorSyPrice.setMinArea(BigDecimalUtils.stringToBigDecimal(twoFloorSyMinArea));   //最小面积
                overAreaTwoFloorSyPrice.setMaxArea(BigDecimalUtils.stringToBigDecimal(twoFloorSyMaxArea));   //最大面积
                overAreaTwoFloorSyPrice.setPrice(BigDecimalUtils.stringToBigDecimal(twoFloorSyPrice));   //补偿单价
                overAreaTwoFloorSyPrice.setSortNum(Integer.valueOf(twoFloorSySortNum));    //排序号
                overAreaTwoFloorSyPrice.setParameter(parameter);   //征收项目
                this.overAreaTwoFloorSyPriceService.save(overAreaTwoFloorSyPrice);
            }
        }


        //保存之前先删除数据【商场扩购】
        this.overAreaScPriceService.deleteOverAreaScPriceByParameterId(parameter.getId());

        String[] scMinAreaArray = ParamUtils.getStringParameters(request,"scMinArea");    //最小面积
        String[] scMaxAreaArray = ParamUtils.getStringParameters(request,"scMaxArea");    //最大面积
        String[] scPriceArray = ParamUtils.getStringParameters(request,"scPrice");    //购房单价
        String[] scSortNumArray = ParamUtils.getStringParameters(request,"scSortNum");    //排序号
        //必须不为空，且长度一致（必填）
        Precondition.checkAjaxArguement(scMinAreaArray != null && scMaxAreaArray != null && scMinAreaArray.length == scMaxAreaArray.length,"2222","面积区间数据填写不规范！");
        int sclength = scMinAreaArray.length;
        String scMinArea = null,scMaxArea = null,scPrice = null,scSortNum = null;
        for (int i = 0; i < sclength; i++){
            scMinArea = null;scMaxArea = null;scPrice = null;scSortNum = null;
            scMinArea = scMinAreaArray[i];scMaxArea = scMaxAreaArray[i];scPrice = scPriceArray[i];scSortNum = scSortNumArray[i];
            if (scMinArea != null && !scMinArea.equals("") && scMaxArea != null && !scMaxArea.equals("") && scPrice != null && !scPrice.equals("") && scSortNum != null && !scSortNum.equals("")){
                OverAreaScPrice overAreaScPrice = new OverAreaScPrice();
                overAreaScPrice.setMinArea(BigDecimalUtils.stringToBigDecimal(scMinArea));   //最小面积
                overAreaScPrice.setMaxArea(BigDecimalUtils.stringToBigDecimal(scMaxArea));   //最大面积
                overAreaScPrice.setPrice(BigDecimalUtils.stringToBigDecimal(scPrice));   //补偿单价
                overAreaScPrice.setSortNum(Integer.valueOf(scSortNum));    //排序号
                overAreaScPrice.setParameter(parameter);   //征收项目
                this.overAreaScPriceService.save(overAreaScPrice);
            }
        }

        //保存之前先删除数据【写字楼扩购】
        this.overAreaXzlPriceService.deleteOverAreaXzlPriceByParameterId(parameter.getId());

        String[] xzlMinAreaArray = ParamUtils.getStringParameters(request,"xzlMinArea");    //最小面积
        String[] xzlMaxAreaArray = ParamUtils.getStringParameters(request,"xzlMaxArea");    //最大面积
        String[] xzlPriceArray = ParamUtils.getStringParameters(request,"xzlPrice");    //购房单价
        String[] xzlSortNumArray = ParamUtils.getStringParameters(request,"xzlSortNum");    //排序号
        //必须不为空，且长度一致（必填）
        Precondition.checkAjaxArguement(xzlMinAreaArray != null && xzlMaxAreaArray != null && xzlMinAreaArray.length == xzlMaxAreaArray.length,"2222","面积区间数据填写不规范！");
        int xzllength = xzlMinAreaArray.length;
        String xzlMinArea = null,xzlMaxArea = null,xzlPrice = null,xzlSortNum = null;
        for (int i = 0; i < xzllength; i++){
            xzlMinArea = null;xzlMaxArea = null;xzlPrice = null;xzlSortNum = null;
            xzlMinArea = xzlMinAreaArray[i];xzlMaxArea = xzlMaxAreaArray[i];xzlPrice = xzlPriceArray[i];xzlSortNum = xzlSortNumArray[i];
            if (xzlMinArea != null && !xzlMinArea.equals("") && xzlMaxArea != null && !xzlMaxArea.equals("") && xzlPrice != null && !xzlPrice.equals("") && xzlSortNum != null && !xzlSortNum.equals("")){
                OverAreaXzlPrice overAreaXzlPrice = new OverAreaXzlPrice();
                overAreaXzlPrice.setMinArea(BigDecimalUtils.stringToBigDecimal(xzlMinArea));   //最小面积
                overAreaXzlPrice.setMaxArea(BigDecimalUtils.stringToBigDecimal(xzlMaxArea));   //最大面积
                overAreaXzlPrice.setPrice(BigDecimalUtils.stringToBigDecimal(xzlPrice));   //补偿单价
                overAreaXzlPrice.setSortNum(Integer.valueOf(xzlSortNum));    //排序号
                overAreaXzlPrice.setParameter(parameter);   //征收项目
                this.overAreaXzlPriceService.save(overAreaXzlPrice);
            }
        }

        //保存之前先删除数据【商业扩购信息】
        this.syKgInfoService.deleteSyKgInfoByParameterId(parameter.getId());

        String[] syKgBuildHouseInfoArray = ParamUtils.getStringParameters(request,"syKgBuildHouseInfo");    //商业情况明细
        String[] syKgKgNameArray = ParamUtils.getStringParameters(request,"syKgKgName");    //商业扩购名称
        String[] syKgAreaArray = ParamUtils.getStringParameters(request,"syKgArea");    //对应面积
        String[] syKgPriceArray = ParamUtils.getStringParameters(request,"syKgPrice");    //商业购买单价
        String[] syKgMoneyArray = ParamUtils.getStringParameters(request,"syKgMoney");    //商业购买金额
        String[] syKgSortNumArray = ParamUtils.getStringParameters(request,"syKgSortNum");    //排序号
        String[] syKgZsAzAreaArray = ParamUtils.getStringParameters(request,"syKgZsAzArea");    //原征收安置面积
        //必须不为空，且长度一致（必填）
        Precondition.checkAjaxArguement(syKgAreaArray != null && syKgMoneyArray != null && syKgAreaArray.length == syKgMoneyArray.length,"2222","单价或金额数据填写不规范！");
        int syKgAreaArraylength = syKgAreaArray.length;
        String syKgBuildHouseInfo = null,syKgKgName = null,syKgArea = null,syKgPrice = null,syKgMoney = null,syKgSortNum = null,zsAzArea = null;
        for (int i = 0; i < syKgAreaArraylength; i++){
            syKgBuildHouseInfo = null;syKgKgName = null;syKgArea = null;syKgPrice = null;syKgMoney = null;syKgSortNum = null;zsAzArea = null;
            syKgBuildHouseInfo = syKgBuildHouseInfoArray[i];
            syKgKgName = syKgKgNameArray[i];
            syKgArea = syKgAreaArray[i];
            syKgPrice = syKgPriceArray[i];
            syKgMoney = syKgMoneyArray[i];
            syKgSortNum = syKgSortNumArray[i];
            zsAzArea = syKgZsAzAreaArray[i];
            if (syKgKgName != null && !syKgKgName.equals("") && syKgBuildHouseInfo != null && !syKgBuildHouseInfo.equals("")
                    && syKgArea != null && !syKgArea.equals("") && syKgPrice != null && !syKgPrice.equals("")
                    && syKgMoney != null && !syKgMoney.equals("") && syKgSortNum != null && !syKgSortNum.equals("")){
                SyKgInfo syKgInfo = new SyKgInfo();
                syKgInfo.setBuildHouseInfo(syKgBuildHouseInfo);//
                syKgInfo.setKgName(syKgKgName);
                syKgInfo.setArea(BigDecimalUtils.stringToBigDecimal(syKgArea));
                syKgInfo.setPrice(BigDecimalUtils.stringToBigDecimal(syKgPrice));
                syKgInfo.setMoney(BigDecimalUtils.stringToBigDecimal(syKgMoney));
                syKgInfo.setSortNum(Integer.valueOf(syKgSortNum));
                syKgInfo.setZsAzArea(zsAzArea);
                syKgInfo.setParameter(parameter);   //对应配置信息表
                this.syKgInfoService.save(syKgInfo);
            }
        }
        return new Response("0001", "保存成功!");
    }

    /**
     * 商业参数信息配置保存
     * @param request
     * @return
     */
    public synchronized Response dataFormSySave(HttpServletRequest request){
        int parameterId = ParamUtils.getInt(request, "parameterId", 0);
        int contractId = ParamUtils.getInt(request, "contractId", 0);
        Parameter parameter = this.findOne(parameterId);
        Contract contract = this.contractService.findOne(contractId);
        if (parameter == null) {
            parameter = new Parameter();
        }
        WebUtils.bind(request, parameter);

        Precondition.checkAjaxArguement(parameter != null,"1111","未找到征收项目，保存失败！");
        Precondition.checkAjaxArguement(contract != null,"1111","未找到征收项目，保存失败！");

        this.save(parameter);
        //保存之前先删除数据【住宅扩购】
        this.overAreaPriceNewService.deleteOverAreaNewPriceByParameterId(parameter.getId());

        String[] minAreaArray = ParamUtils.getStringParameters(request,"minArea");    //最小面积
        String[] maxAreaArray = ParamUtils.getStringParameters(request,"maxArea");    //最大面积
        String[] priceArray = ParamUtils.getStringParameters(request,"price");    //购房单价
        String[] sortNumArray = ParamUtils.getStringParameters(request,"sortNum");    //排序号
        //必须不为空，且长度一致（必填）
        Precondition.checkAjaxArguement(minAreaArray != null && maxAreaArray != null && minAreaArray.length == maxAreaArray.length,"2222","面积区间数据填写不规范！");
        int length = minAreaArray.length;
        String minArea = null,maxArea = null,price = null,sortNum = null;
        for (int i = 0; i < length; i++){
            minArea = null;maxArea = null;price = null;sortNum = null;
            minArea = minAreaArray[i];maxArea = maxAreaArray[i];price = priceArray[i];sortNum = sortNumArray[i];
            if (minArea != null && !minArea.equals("") && maxArea != null && !maxArea.equals("") && price != null && !price.equals("") && sortNum != null && !sortNum.equals("")){
                OverAreaPriceNew overAreaPrice = new OverAreaPriceNew();
                overAreaPrice.setMinArea(BigDecimalUtils.stringToBigDecimal(minArea));   //最小面积
                overAreaPrice.setMaxArea(BigDecimalUtils.stringToBigDecimal(maxArea));   //最大面积
                overAreaPrice.setPrice(BigDecimalUtils.stringToBigDecimal(price));   //补偿单价
                overAreaPrice.setSortNum(Integer.valueOf(sortNum));    //排序号
                overAreaPrice.setParameter(parameter);   //征收项目
                this.overAreaPriceNewService.save(overAreaPrice);
            }
        }

        //保存之前先删除数据【一层商业扩购】
        this.overAreaOneFloorSyPriceService.deleteOneFloorSyByParameterId(parameter.getId());

        String[] oneFloorSyMinAreaArray = ParamUtils.getStringParameters(request,"oneFloorSyMinArea");    //最小面积
        String[] oneFloorSyMaxAreaArray = ParamUtils.getStringParameters(request,"oneFloorSyMaxArea");    //最大面积
        String[] oneFloorSyPriceArray = ParamUtils.getStringParameters(request,"oneFloorSyPrice");    //购房单价
        String[] oneFloorSySortNumArray = ParamUtils.getStringParameters(request,"oneFloorSySortNum");    //排序号
        //必须不为空，且长度一致（必填）
        Precondition.checkAjaxArguement(oneFloorSyMinAreaArray != null && oneFloorSyMaxAreaArray != null && oneFloorSyMinAreaArray.length == oneFloorSyMaxAreaArray.length,"2222","面积区间数据填写不规范！");
        int oneFloorSylength = oneFloorSyMinAreaArray.length;
        String oneFloorSyMinArea = null,oneFloorSyMaxArea = null,oneFloorSyPrice = null,oneFloorSySortNum = null;
        for (int i = 0; i < oneFloorSylength; i++){
            oneFloorSyMinArea = null;oneFloorSyMaxArea = null;oneFloorSyPrice = null;oneFloorSySortNum = null;
            oneFloorSyMinArea = oneFloorSyMinAreaArray[i];oneFloorSyMaxArea = oneFloorSyMaxAreaArray[i];oneFloorSyPrice = oneFloorSyPriceArray[i];oneFloorSySortNum = oneFloorSySortNumArray[i];
            if (oneFloorSyMinArea != null && !oneFloorSyMinArea.equals("") && oneFloorSyMaxArea != null && !oneFloorSyMaxArea.equals("") && oneFloorSyPrice != null && !oneFloorSyPrice.equals("") && oneFloorSySortNum != null && !oneFloorSySortNum.equals("")){
                OverAreaOneFloorSyPrice overAreaOneFloorSyPrice = new OverAreaOneFloorSyPrice();
                overAreaOneFloorSyPrice.setMinArea(BigDecimalUtils.stringToBigDecimal(oneFloorSyMinArea));   //最小面积
                overAreaOneFloorSyPrice.setMaxArea(BigDecimalUtils.stringToBigDecimal(oneFloorSyMaxArea));   //最大面积
                overAreaOneFloorSyPrice.setPrice(BigDecimalUtils.stringToBigDecimal(oneFloorSyPrice));   //补偿单价
                overAreaOneFloorSyPrice.setSortNum(Integer.valueOf(oneFloorSySortNum));    //排序号
                overAreaOneFloorSyPrice.setParameter(parameter);   //征收项目
                this.overAreaOneFloorSyPriceService.save(overAreaOneFloorSyPrice);
            }
        }

        //保存之前先删除数据【二层商业扩购】
        this.overAreaTwoFloorSyPriceService.deleteOverAreaTwoFloorSyPriceByParameterId(parameter.getId());

        String[] twoFloorSyMinAreaArray = ParamUtils.getStringParameters(request,"twoFloorSyMinArea");    //最小面积
        String[] twoFloorSyMaxAreaArray = ParamUtils.getStringParameters(request,"twoFloorSyMaxArea");    //最大面积
        String[] twoFloorSyPriceArray = ParamUtils.getStringParameters(request,"twoFloorSyPrice");    //购房单价
        String[] twoFloorSySortNumArray = ParamUtils.getStringParameters(request,"twoFloorSySortNum");    //排序号
        //必须不为空，且长度一致（必填）
        Precondition.checkAjaxArguement(twoFloorSyMinAreaArray != null && twoFloorSyMaxAreaArray != null && twoFloorSyMinAreaArray.length == twoFloorSyMaxAreaArray.length,"2222","面积区间数据填写不规范！");
        int twoFloorSylength = twoFloorSyMinAreaArray.length;
        String twoFloorSyMinArea = null,twoFloorSyMaxArea = null,twoFloorSyPrice = null,twoFloorSySortNum = null;
        for (int i = 0; i < twoFloorSylength; i++){
            twoFloorSyMinArea = null;twoFloorSyMaxArea = null;twoFloorSyPrice = null;twoFloorSySortNum = null;
            twoFloorSyMinArea = twoFloorSyMinAreaArray[i];twoFloorSyMaxArea = twoFloorSyMaxAreaArray[i];twoFloorSyPrice = twoFloorSyPriceArray[i];twoFloorSySortNum = twoFloorSySortNumArray[i];
            if (twoFloorSyMinArea != null && !twoFloorSyMinArea.equals("") && twoFloorSyMaxArea != null && !twoFloorSyMaxArea.equals("") && twoFloorSyPrice != null && !twoFloorSyPrice.equals("") && twoFloorSySortNum != null && !twoFloorSySortNum.equals("")){
                OverAreaTwoFloorSyPrice overAreaTwoFloorSyPrice = new OverAreaTwoFloorSyPrice();
                overAreaTwoFloorSyPrice.setMinArea(BigDecimalUtils.stringToBigDecimal(twoFloorSyMinArea));   //最小面积
                overAreaTwoFloorSyPrice.setMaxArea(BigDecimalUtils.stringToBigDecimal(twoFloorSyMaxArea));   //最大面积
                overAreaTwoFloorSyPrice.setPrice(BigDecimalUtils.stringToBigDecimal(twoFloorSyPrice));   //补偿单价
                overAreaTwoFloorSyPrice.setSortNum(Integer.valueOf(twoFloorSySortNum));    //排序号
                overAreaTwoFloorSyPrice.setParameter(parameter);   //征收项目
                this.overAreaTwoFloorSyPriceService.save(overAreaTwoFloorSyPrice);
            }
        }


        //保存之前先删除数据【商场扩购】
        this.overAreaScPriceService.deleteOverAreaScPriceByParameterId(parameter.getId());

        String[] scMinAreaArray = ParamUtils.getStringParameters(request,"scMinArea");    //最小面积
        String[] scMaxAreaArray = ParamUtils.getStringParameters(request,"scMaxArea");    //最大面积
        String[] scPriceArray = ParamUtils.getStringParameters(request,"scPrice");    //购房单价
        String[] scSortNumArray = ParamUtils.getStringParameters(request,"scSortNum");    //排序号
        //必须不为空，且长度一致（必填）
        Precondition.checkAjaxArguement(scMinAreaArray != null && scMaxAreaArray != null && scMinAreaArray.length == scMaxAreaArray.length,"2222","面积区间数据填写不规范！");
        int sclength = scMinAreaArray.length;
        String scMinArea = null,scMaxArea = null,scPrice = null,scSortNum = null;
        for (int i = 0; i < sclength; i++){
            scMinArea = null;scMaxArea = null;scPrice = null;scSortNum = null;
            scMinArea = scMinAreaArray[i];scMaxArea = scMaxAreaArray[i];scPrice = scPriceArray[i];scSortNum = scSortNumArray[i];
            if (scMinArea != null && !scMinArea.equals("") && scMaxArea != null && !scMaxArea.equals("") && scPrice != null && !scPrice.equals("") && scSortNum != null && !scSortNum.equals("")){
                OverAreaScPrice overAreaScPrice = new OverAreaScPrice();
                overAreaScPrice.setMinArea(BigDecimalUtils.stringToBigDecimal(scMinArea));   //最小面积
                overAreaScPrice.setMaxArea(BigDecimalUtils.stringToBigDecimal(scMaxArea));   //最大面积
                overAreaScPrice.setPrice(BigDecimalUtils.stringToBigDecimal(scPrice));   //补偿单价
                overAreaScPrice.setSortNum(Integer.valueOf(scSortNum));    //排序号
                overAreaScPrice.setParameter(parameter);   //征收项目
                this.overAreaScPriceService.save(overAreaScPrice);
            }
        }

        //保存之前先删除数据【写字楼扩购】
        this.overAreaXzlPriceService.deleteOverAreaXzlPriceByParameterId(parameter.getId());

        String[] xzlMinAreaArray = ParamUtils.getStringParameters(request,"xzlMinArea");    //最小面积
        String[] xzlMaxAreaArray = ParamUtils.getStringParameters(request,"xzlMaxArea");    //最大面积
        String[] xzlPriceArray = ParamUtils.getStringParameters(request,"xzlPrice");    //购房单价
        String[] xzlSortNumArray = ParamUtils.getStringParameters(request,"xzlSortNum");    //排序号
        //必须不为空，且长度一致（必填）
        Precondition.checkAjaxArguement(xzlMinAreaArray != null && xzlMaxAreaArray != null && xzlMinAreaArray.length == xzlMaxAreaArray.length,"2222","面积区间数据填写不规范！");
        int xzllength = xzlMinAreaArray.length;
        String xzlMinArea = null,xzlMaxArea = null,xzlPrice = null,xzlSortNum = null;
        for (int i = 0; i < xzllength; i++){
            xzlMinArea = null;xzlMaxArea = null;xzlPrice = null;xzlSortNum = null;
            xzlMinArea = xzlMinAreaArray[i];xzlMaxArea = xzlMaxAreaArray[i];xzlPrice = xzlPriceArray[i];xzlSortNum = xzlSortNumArray[i];
            if (xzlMinArea != null && !xzlMinArea.equals("") && xzlMaxArea != null && !xzlMaxArea.equals("") && xzlPrice != null && !xzlPrice.equals("") && xzlSortNum != null && !xzlSortNum.equals("")){
                OverAreaXzlPrice overAreaXzlPrice = new OverAreaXzlPrice();
                overAreaXzlPrice.setMinArea(BigDecimalUtils.stringToBigDecimal(xzlMinArea));   //最小面积
                overAreaXzlPrice.setMaxArea(BigDecimalUtils.stringToBigDecimal(xzlMaxArea));   //最大面积
                overAreaXzlPrice.setPrice(BigDecimalUtils.stringToBigDecimal(xzlPrice));   //补偿单价
                overAreaXzlPrice.setSortNum(Integer.valueOf(xzlSortNum));    //排序号
                overAreaXzlPrice.setParameter(parameter);   //征收项目
                this.overAreaXzlPriceService.save(overAreaXzlPrice);
            }
        }

        //保存之前先删除数据【写字楼扩购】
        this.syKgInfoService.deleteSyKgInfoByParameterId(parameter.getId());

        String[] syKgBuildHouseInfoArray = ParamUtils.getStringParameters(request,"syKgBuildHouseInfo");    //商业情况明细
        String[] syKgKgNameArray = ParamUtils.getStringParameters(request,"syKgKgName");    //商业扩购名称
        String[] syKgAreaArray = ParamUtils.getStringParameters(request,"syKgArea");    //对应面积
        String[] syKgPriceArray = ParamUtils.getStringParameters(request,"syKgPrice");    //商业购买单价
        String[] syKgMoneyArray = ParamUtils.getStringParameters(request,"syKgMoney");    //商业购买金额
        String[] syKgSortNumArray = ParamUtils.getStringParameters(request,"syKgSortNum");    //排序号
        String[] syKgZsAzAreaArray = ParamUtils.getStringParameters(request,"syKgZsAzArea");    //原征收安置面积
        //必须不为空，且长度一致（必填）
        Precondition.checkAjaxArguement(syKgAreaArray != null && syKgMoneyArray != null && syKgAreaArray.length == syKgMoneyArray.length,"2222","单价或金额数据填写不规范！");
        int syKgAreaArraylength = syKgAreaArray.length;
        String syKgBuildHouseInfo = null,syKgKgName = null,syKgArea = null,syKgPrice = null,syKgMoney = null,syKgSortNum = null,zsAzArea = null;
        for (int i = 0; i < syKgAreaArraylength; i++){
            syKgBuildHouseInfo = null;syKgKgName = null;syKgArea = null;syKgPrice = null;syKgMoney = null;syKgSortNum = null;zsAzArea = null;
            syKgBuildHouseInfo = syKgBuildHouseInfoArray[i];
            syKgKgName = syKgKgNameArray[i];
            syKgArea = syKgAreaArray[i];
            syKgPrice = syKgPriceArray[i];
            syKgMoney = syKgMoneyArray[i];
            syKgSortNum = syKgSortNumArray[i];
            zsAzArea = syKgZsAzAreaArray[i];
            if (syKgKgName != null && !syKgKgName.equals("") && syKgBuildHouseInfo != null && !syKgBuildHouseInfo.equals("")
                    && syKgArea != null && !syKgArea.equals("") && syKgPrice != null && !syKgPrice.equals("")
                    && syKgMoney != null && !syKgMoney.equals("") && syKgSortNum != null && !syKgSortNum.equals("")){
                SyKgInfo syKgInfo = new SyKgInfo();
                syKgInfo.setBuildHouseInfo(syKgBuildHouseInfo);//
                syKgInfo.setKgName(syKgKgName);
                syKgInfo.setArea(BigDecimalUtils.stringToBigDecimal(syKgArea));
                syKgInfo.setPrice(BigDecimalUtils.stringToBigDecimal(syKgPrice));
                syKgInfo.setMoney(BigDecimalUtils.stringToBigDecimal(syKgMoney));
                syKgInfo.setSortNum(Integer.valueOf(syKgSortNum));
                syKgInfo.setZsAzArea(zsAzArea);
                syKgInfo.setParameter(parameter);   //对应配置信息表
                this.syKgInfoService.save(syKgInfo);
            }
        }

        contract.setParameter(parameter);
        this.contractService.save(contract);
        return new Response("0001", "保存成功!");
    }

    /**
     * 通过配置表id 获取扩购区间关键面积（只针对有两个扩购区间的）
     * @param parameterId
     * @return
     */
    public BigDecimal getOverArea(Integer parameterId) {
        StringBuffer sql = new StringBuffer(" SELECT MIN(o.max_area) FROM over_area_price_new o WHERE o.parameter_id =:parameterId ");
        Map<String, Object> map = Maps.newLinkedHashMap();
        map.put("parameterId", parameterId);
        return BigDecimalUtils.stringToBigDecimal(String.valueOf(this.findSqlObject(sql.toString(), map).get(0)));
    }

    /**
     * 新增扩购区间
     * @param request
     * @return
     * @throws IllegalAccessException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     */
    public synchronized Response setSaveOverArea(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        String overAreaStr = ParamUtils.getString(request, "overArea", "0");
        BigDecimal overArea = BigDecimalUtils.stringToBigDecimal(overAreaStr);//节点面积
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "0012", "找不到该户信息！");
        Parameter parameter = this.findOne(contract.getParameter().getId());//本协议对应的配置表
        //若执行此方法需新clone一条数据
        Parameter parameterItem = this.cloneParameter(parameter,contract);
        parameterItem.setId(null);//clone 过来的数据Id要清空，不然会报错
        this.save(parameterItem);
        Precondition.checkAjaxArguement(overArea.compareTo(BigDecimal.ZERO) >= 0, "0012", "此处不可填入小于0的数值！");
        if (overArea.compareTo(BigDecimal.ZERO) == 0) {//若等于0则无节点面积  直接9500
            OverAreaPriceNew overAreaPriceNew = new OverAreaPriceNew();
            //仅需插入1条
            overAreaPriceNew.setParameter(parameterItem);
            overAreaPriceNew.setMinArea(BigDecimal.ZERO);
            overAreaPriceNew.setMaxArea(new BigDecimal("9999"));
            overAreaPriceNew.setPrice(new BigDecimal("9100"));
            overAreaPriceNew.setSortNum(10);
            this.overAreaPriceNewService.save(overAreaPriceNew);
        } else if (overArea.compareTo(BigDecimal.ZERO) > 0) {
            //第1条
            OverAreaPriceNew overAreaPriceNewOne = new OverAreaPriceNew();
            overAreaPriceNewOne.setParameter(parameterItem);
            overAreaPriceNewOne.setMinArea(BigDecimal.ZERO);
            overAreaPriceNewOne.setMaxArea(overArea);
            overAreaPriceNewOne.setPrice(new BigDecimal("3800"));
            overAreaPriceNewOne.setSortNum(10);
            this.overAreaPriceNewService.save(overAreaPriceNewOne);
            //第2条
            OverAreaPriceNew overAreaPriceNewTwo = new OverAreaPriceNew();
            overAreaPriceNewTwo.setParameter(parameterItem);
            overAreaPriceNewTwo.setMinArea(overArea);
            overAreaPriceNewTwo.setMaxArea(new BigDecimal("9999"));
            overAreaPriceNewTwo.setPrice(new BigDecimal("9100"));
            overAreaPriceNewTwo.setSortNum(20);
            this.overAreaPriceNewService.save(overAreaPriceNewTwo);
        }
        //将新配置的表付值到当前协议中
        contract.setParameter(parameterItem);
        this.contractService.save(contract);
        CalculateUtils.main(contract.getId());

        Response response = new Response();
        response.setCode("0001");
        return response;
    }

    /**
     * 配置表添加
     * @param request
     * @return
     * @throws IllegalAccessException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     */
    public synchronized Parameter saveParameter(HttpServletRequest request) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Integer contractId = ParamUtils.getInt(request, "contractId", 0);
        String overAreaStr = ParamUtils.getString(request, "overArea", "0");
        BigDecimal overArea = BigDecimalUtils.stringToBigDecimal(overAreaStr);//节点面积
        Contract contract = this.contractService.findOne(contractId);
        Precondition.checkAjaxArguement(contract != null, "0012", "找不到该户信息！");
        Parameter parameter = this.findOne(contract.getParameter().getId());//本协议对应的配置表
        //若执行此方法需新clone一条数据
        Parameter parameterItem = this.cloneParameter(parameter,contract);
        parameterItem.setId(null);//clone 过来的数据Id要清空，不然会报错
        this.save(parameterItem);
        Precondition.checkAjaxArguement(overArea.compareTo(BigDecimal.ZERO) >= 0, "0012", "此处不可填入小于0的数值！");
        if (overArea.compareTo(BigDecimal.ZERO) == 0) {//若等于0则无节点面积  直接9500
            OverAreaPriceNew overAreaPriceNew = new OverAreaPriceNew();
            //仅需插入1条
            overAreaPriceNew.setParameter(parameterItem);
            overAreaPriceNew.setMinArea(BigDecimal.ZERO);
            overAreaPriceNew.setMaxArea(new BigDecimal("9999"));
            overAreaPriceNew.setPrice(new BigDecimal("9100"));
            overAreaPriceNew.setSortNum(10);
            this.overAreaPriceNewService.save(overAreaPriceNew);
        } else if (overArea.compareTo(BigDecimal.ZERO) > 0) {
            //第1条
            OverAreaPriceNew overAreaPriceNewOne = new OverAreaPriceNew();
            overAreaPriceNewOne.setParameter(parameterItem);
            overAreaPriceNewOne.setMinArea(BigDecimal.ZERO);
            overAreaPriceNewOne.setMaxArea(overArea);
            overAreaPriceNewOne.setPrice(new BigDecimal("3800"));
            overAreaPriceNewOne.setSortNum(10);
            this.overAreaPriceNewService.save(overAreaPriceNewOne);
            //第2条
            OverAreaPriceNew overAreaPriceNewTwo = new OverAreaPriceNew();
            overAreaPriceNewTwo.setParameter(parameterItem);
            overAreaPriceNewTwo.setMinArea(overArea);
            overAreaPriceNewTwo.setMaxArea(new BigDecimal("9999"));
            overAreaPriceNewTwo.setPrice(new BigDecimal("9100"));
            overAreaPriceNewTwo.setSortNum(20);
            this.overAreaPriceNewService.save(overAreaPriceNewTwo);
        }
        //将新配置的表付值到当前协议中
        contract.setParameter(parameterItem);
        this.contractService.save(contract);
        return parameterItem;
    }

    /**
     * 使用 Spring BeanUtils（Spring 框架的一部分）
     * @param parameterItem
     * @param contract
     * @return
     */
    public  Parameter cloneParameter(Parameter parameterItem,Contract contract) {
        Parameter parameterSon = new Parameter();
        try {
            BeanUtils.copyProperties(parameterItem,parameterSon);
        } catch (Exception e) {

        }
        //格式化当前日期后再存入系统
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        String formattedDate = sdf.format(now);
        //配置参数名称
        parameterSon.setName(formattedDate + "_【" + contract.getId() + "】_【" + contract.getContractCode() + "】");
        return parameterSon;
    }


}
